import Watcher from "../../watcher/index.js"

export default function initComputed(vm) {
    const computed = vm.$computed
    for (let key in computed) {
        const wathcer = vm._watcher = Object.create(null)
        wathcer[key] = new Watcher(computed[key], { lazy: true }, vm)
        defineComputed(vm, key)
    }
}

/* 将computed属性代理到vue实例上 */
function defineComputed(vm, key) {
    Object.defineProperty(vm, key, {
        get() {
            const watcher = vm._watcher[key]
            if (watcher.dirty) {
                // 更新数据
                // console.log("getter doubleCounter", watcher.dirty)
                watcher.evaluate()
                return watcher.value
            }
            // 此处获取的就是缓存中的数据
            // console.log("getter doubleCounter", watcher.dirty)
            return watcher.value
        },
        set() {
            console.warn("not setter!!!")
        }
    })
}